#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
using ll = int64_t;

const ll maxn = 10'0000 + 5, inf = 1e9 + 7;
struct C {
    ll d, c, a;
    inline bool operator<(const C &o) const {
        return d < o.d;
    }
} c[maxn];
ll n, g, ans;
std::map<ll, ll> cow_num, num_cowcount;

int main() {
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> n >> g;
    for (ll i = 1; i <= n; i++) {
        std::cin >> c[i].d >> c[i].c >> c[i].a;
    }
    std::sort(c + 1, c + 1 + n);

    num_cowcount[0] = inf;

    for (ll i = 1; i <= n; i++) {

        ll max_num_before = num_cowcount.rbegin()->first;

        ll old_cownum = cow_num[c[i].c];
        ll wasmaxcnt = num_cowcount[old_cownum];
        bool was_top = (old_cownum == max_num_before);

        num_cowcount[old_cownum]--;
        if (num_cowcount[old_cownum] == 0) {
            num_cowcount.erase(old_cownum);
        }

        ll new_cownum = old_cownum + c[i].a;
        cow_num[c[i].c] = new_cownum;

        num_cowcount[new_cownum]++;
        ll ismaxcnt = num_cowcount[new_cownum];
        ll max_num_after = num_cowcount.rbegin()->first;

        bool is_top = (new_cownum == max_num_after);

        if ((was_top != is_top)||(was_top&is_top && wasmaxcnt!=ismaxcnt)) {
            ans++;
        }
    }
    std::cout << ans << "\n";
}